package com.xxx.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.xxx.constant.RedisConstants;
import com.xxx.constant.SystemConstants;

/**
 * 通用工具类
 * 
 * 
 * 
 */
public class CommonUtil {

	private static final int SMS_CODE_LENGTH = 6;		// 短信验证码长度
	private static final String SMS_CODE ="0123456789";	// 短信验证码数字范围
	private static final String DEFAULT_VERICODE = "918819";  // 测试用短信验证码
	
	/**
	 * 生成短信验证码
	 */
	public static String generateSmsVeriCode() {
		int len = SMS_CODE.length();
		Random ran = new Random(System.currentTimeMillis());
		StringBuffer code = new StringBuffer();
		for(int i = 0; i<SMS_CODE_LENGTH; i++) {
			code.append(SMS_CODE.charAt(ran.nextInt(len-1)));
		}
		return code.toString();
	}
	
	/**
	 * 校验短信验证码是否正确
	 */
	public static boolean chkSmsVeriCode(String userMobile, String veriCode) {
		if(DEFAULT_VERICODE.equals(veriCode)) {
			return true;
		}
		String redisKey = MessageFormat.format(RedisConstants.REDIS_KEY_VERICODE_SMS, userMobile);
    	String redisValue = JedisUtil.get(redisKey);
    	if(!StringUtils.isEmpty(redisValue) && redisValue.equals(veriCode)) { // redis中短信验证码未过期，并且相同，则验证通过
//    		JedisUtil.del(redisKey); // 校验之后，删除redis中的key
    		return true;
    	}
		return false;
	}
	
	/**
	 * 获取不带-的UUID
	 */
	public static String getUUID(){
	    return UUID.randomUUID().toString().replace("-", "");
	}
	
	/**
	 * 判断当前操作系统是否为windows
	 */
	public static boolean isWin() {
		Properties props = System.getProperties();
		String osName = props.getProperty("os.name");
		if(osName.toLowerCase().indexOf("windows") != -1) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断当前操作系统是否为linux
	 */
	public static boolean isLinux() {
		Properties props = System.getProperties();
		String osName = props.getProperty("os.name");
		if(osName.toLowerCase().indexOf("linux") != -1) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断密码安全级别
	 */
	public static String chkPassLevel(String pass) {
		String regexZ = "\\d*";	// 纯数字
		String regexS = "[a-zA-Z]+";  // 纯字母
		String regexT = "\\W+$";	// 数字+字母
		String regexZT = "\\D*";	// 
		String regexST = "[\\d\\W]*";  // 
		String regexZS = "\\w*";
		String regexZST = "[\\w\\W]*";
 
		if (pass.matches(regexZ)) {
			return SystemConstants.PASS_LEVEL_LOW;
		} else if (pass.matches(regexS)) {
			return SystemConstants.PASS_LEVEL_LOW;
		} else if (pass.matches(regexT)) {
			return SystemConstants.PASS_LEVEL_LOW;
		} else if (pass.matches(regexZT)) {
			return SystemConstants.PASS_LEVEL_MID;
		} else if (pass.matches(regexST)) {
			return SystemConstants.PASS_LEVEL_MID;
		} else if (pass.matches(regexZS)) {
			return SystemConstants.PASS_LEVEL_MID;
		} else if (pass.matches(regexZST)) {
			return SystemConstants.PASS_LEVEL_HIGH;
		}
		return SystemConstants.PASS_LEVEL_LOW;
	}

	/**
	 * 校验BigDecimal是否小于0
	 * @param bigDec
	 * @return true：小于或等于0；false：大于0
	 */
	public static boolean chkBigDecSmallerThanZero(BigDecimal bigDec) {
		return bigDec == null || bigDec.compareTo(BigDecimal.ZERO)<=0 || bigDec.doubleValue()<=0;
	}
	
	/**
	 * 校验BigInteger是否小于0
	 * @param bigDec
	 * @return true：小于或等于0；false：大于0
	 */
	public static boolean chkBigIntSmallerThanZero(BigInteger bigInt) {
		return bigInt == null || bigInt.compareTo(BigInteger.ZERO)<=0 || bigInt.intValue()<=0;
	}
	
	/**
	 * 获取中文时分秒的时长（单位：秒）
	 */
	public static long getSeconds(String time) {
		String str = time.replace("天", ":").replace("小时", ":")
						.replace("时", ":").replace("分钟", ":").replace("分", ":").replace("秒钟", "").replace("秒", "");
		String[] hmsArray = str.split(":");
		long count = 0;
		if(hmsArray.length==3) {
			count = Long.valueOf(hmsArray[0]) * 3600 + Long.valueOf(hmsArray[1]) * 60 + Long.valueOf(hmsArray[2]);
		} else if(hmsArray.length==4) {
			count = Long.valueOf(hmsArray[0]) * 3600 * 24 + Long.valueOf(hmsArray[1]) * 3600 + Long.valueOf(hmsArray[2]) * 60 + Long.valueOf(hmsArray[3]);
		}
		System.out.println("时长共：" + count + " 秒！");
		return count;
	}
	
	/**
	 * 判断是否为纯数字（0或正整数）
	 * @param string
	 * @return
	 */
	public static boolean isNumeric(String string){
	    Pattern pattern = Pattern.compile("[0-9]*");
	    return pattern.matcher(string).matches();   
	}
	
	/**
	 * 获取文件名后缀
	 */
	public static String getSuffix(String filePath){
		if(filePath==null || "".equals(filePath.trim())){
			return "";
		}
		if(filePath.contains(".")){
			return filePath.substring(filePath.lastIndexOf(".")+1,filePath.length());
		}
		return "";
	}
	
	/**
	 * 字符串左补齐空格
	 * @param str 原始字符串
	 * @param len 补齐后的总长度
	 * @return
	 */
    public static String leftPad(String str, int len) {
        int addSpaceCnt = len-str.length();
        String space = "";
        for (int k=0; k<addSpaceCnt; k++){
                space= " "+space;
        }
        return space + str;
    }
    
    /**
     * 统计指定字符串list中以A开头的且长度最长的字符的长度
     */
    public static int getMaxLenth(List<String> list) {
    	return list.stream()
    			.filter(e -> e.startsWith("A"))
    			.mapToInt(String::length)
    			.max()
    			.getAsInt();
    }
    
    
    
	public static void main(String[] args) {
		String time = "2天0小时35分钟9秒";
		time = "0小时35分钟9秒";
		getSeconds(time);
		System.out.println(leftPad("abc", 10));
		
		List<String> list = new ArrayList<>();
		list.add("Apple");
		list.add("Baby");
		list.add("Cat");
		list.add("Dog");
		list.add("ABC");
		list.add("Cake");
		System.out.println(getMaxLenth(list));
	}
}
